import 'dart:async';

import 'package:flutter/material.dart';
import 'package:moose_app/core/utils/navigate_util.dart';
import 'package:moose_app/ui/screens/guide/guide_screen.dart';
import 'package:moose_app/ui/values/values.dart';

class ATHSplashBody extends StatefulWidget {
  @override
  _ATHSplashBodyState createState() => _ATHSplashBodyState();
}

class _ATHSplashBodyState extends State<ATHSplashBody> {
  Timer _timer;
  int _count = 5;

  @override
  void initState() {
    super.initState();
    _startTime();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        ConstrainedBox(
          constraints: BoxConstraints.expand(),
          child: Image.asset("assets/images/welcome2.jpg", fit: BoxFit.fill),
        ),
        Positioned(
          top: kSize80,
          right: kSize40,
          child: ClipRRect(
            borderRadius: BorderRadius.all(Radius.circular(kSize8)),
            child: GestureDetector(
              onTap: () {
                _navigationPage();
              },
              child: Container(
                width: kSize180,
                height: kSize50,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                    border: Border.all(
                        color: Colors.white,
                        width: kSize1,
                        style: BorderStyle.solid),
                    borderRadius: BorderRadius.circular(kSize28)),
                child: Text(
                  "跳过广告 $_count",
                  style: TextStyle(color: Colors.white),
                ),
              ),
            ),
          ),
        ),
      ],
    );
  }

  _startTime() async {
    var _duration = Duration(seconds: 1);
    Timer(_duration, () {
      // 空等1秒之后再计时
      _timer = Timer.periodic(const Duration(milliseconds: 1000), (v) {
        _count--;
        if (_count <= 0) {
          if (_timer != null) _timer.cancel();
          _navigationPage();
        } else {
          setState(() {});
        }
      });
      return _timer;
    });
  }

  void _navigationPage() async {
    _timer.cancel();
    ATHNavigator.pushFromRightDuration(context, ATHGuideScreen.routeName);
  }
}
